<?php
namespace app\api\controller\account;

use app\api\controller\Base;
use app\api\controller\Pub;
use app\common\model\account\AccountCreate;
use app\common\model\account\AccountInWarehouse;
use app\common\model\account\AccountOutWarehouse;
use app\common\model\account\AccountLink;
use app\common\model\account\AccountSelfUse;
use app\common\model\account\AccountEntrustUse;
use app\common\model\company\Company;
use think\facade\Request;
use app\common\model\user\User AS UserModel;
//产废单位 产生台账
class Account extends Base
{
    public function initialize()
    {
        parent::initialize();
        self::is_login();
        // Rolepower::powerguard(801001);
    }

    /**
     * 获取基础信息
     * @return [type] [description]
     */
    public function getlist()
    {
        $data = input('post.');
        if(!empty($data['startime'])&&!empty($data['endtime'])){
            //显示区间
            $startime = $data['startime'];//开始时间
            $endtime = $data['endtime'];//结束时间
        }
        //显示代码
        if(!empty($data['goodid'])){
            $goodid = $data['goodid'];
        }
        
        //显示的台账 类型 all汇总 create产生台账 inwarehouse入库台账 ouwarehouse出库 selfuse自利用 entrustuse委外利用
        $type = empty($data['type'])?'all':$data['type'];
        if($type=='all'){
            $forlist = ['create','inwarehouse','outwarehouse','selfuse','entrustuse'];
        }else{
            $forlist = [$type];
        }
        $typename = ['create'=>'创建台账','inwarehouse'=>'入库台账','outwarehouse'=>'出库台账','selfuse'=>'自利用/处置台账','entrustuse'=>'委外利用/处置台账'];
        $company =  Company::user_getinfo();//获取产废单位企业信息
        if(empty($company)){
            return myerr('缺少企业信息');
        }
        $company_id = $company->CompanyId;
        $res = [];
        $alllist = [];
        $alllist_key_arrays = [];
        foreach ($forlist as $key => $value) {
            switch ($value) {
                case 'create':
                    // 产生台账
                    $res[$value] = new AccountCreate;
                    break;
                case 'inwarehouse':
                    // 入库台账
                    $res[$value] = new AccountInWarehouse;
                    break;
                case 'outwarehouse':
                    // 出库台账
                    $res[$value] = new AccountOutWarehouse;
                    break;
                case 'selfuse':
                    // 产生台账
                    $res[$value] = new AccountSelfUse;
                    break;
                case 'entrustuse':
                    // 产生台账
                    $res[$value] = new AccountEntrustUse;
                    break;
            }
            $res[$value]  = $res[$value]::where('CompanyId',$company_id)->order('AccountTime desc')->hidden(['OperationLog']);
            //根据时间选择台账
            if(!empty($startime)&&!empty($endtime)){
                $res[$value] = $res[$value]->where('AccountTime','>=',$startime)->where('AccountTime','<=',$endtime);
            }
            //根据代码选择台账
            if(!empty($goodid)){
                $res[$value] = $res[$value]->where('GoodId','in',$goodid);
            }

            $res[$value] = $res[$value]->with('link,goodinfo')->hidden(['goodinfo.TotalWeight','goodinfo.OperationLog'])->select();
            foreach ($res[$value] as $rk => $rv) {
                $rv['Type'] = $value;
                $rv['TypeName'] = $typename[$value];
                $alllist[] = $rv;
                $alllist_key_arrays[] = $rv['AccountTime'];
            }
        }
        array_multisort($alllist_key_arrays,SORT_DESC,SORT_REGULAR,$alllist);
        $res['alllist'] =$alllist;
        return myok($res);
    }

    /**
     * 修改/完善企业信息
     * 产废人信息也再次完善
     * @return [type] [description]
     */
    public function reinfo()
    {
        $data    = input('post.');
        $company = Coll::user_getinfo();
        //为空则给企业返回修改信息
        if (empty($data)) {
            return myok($company);
        }
        if (empty($company)) {
            //创建一个企业信息
            $result = $this->validate($data, 'app\common\validate\Collection');
            if (true !== $result) {
                return myerr($result);
            }
            $company = Coll::user_create($data);
            return myok('财务信息已登记');
        } else {
            //修改企业信息
            $sb = Coll::user_reinfo($data);
            return myok('财务信息修改成功');
        }
    }

    
    /**
     * 判断账号是否登录
     * @return boolean [description]
     */
    private function is_login()
    {
        $data = UserModel::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
}
